#include <iostream>
#include "hand.h"

using namespace std;

//**********************************************************************************
Hand::Hand(){
	/*hand = new Card[5];*/
	index = 0;
	size = 5;
}

Hand::Hand(int Hsize){
	size = Hsize;
	/*hand = new Card[size];*/
	index = 0;
}

//**********************************************************************************
void Hand::displayHand(){
	for (int i = 0; i < size; i++)
		cout << " -------- ";
	cout << endl;
	
	for (int j = 0; j < size; j++)
		cout << "|" << hand[j] << "      |";
	cout << endl;
	
	for (int k = 0; k < 7; k++){
		for (int l = 0; l < size; l++){
			cout << "|        |";
		}
	cout << endl;
	}
	
	for (int m = 0; m < size; m++)
		cout << "|      " << hand[m] << "|";
	cout << endl;

	for (int n = 0; n < size; n++)
		cout << " -------- ";
	cout << endl;
}

ostream& operator<<(ostream& out, Hand myHand) {
	for (int i = 0; i < myHand.size; i++)
		out << " -------- ";
	out << endl;
	
	for (int j = 0; j < myHand.size; j++)
		out << "|" << myHand.hand[j] << "      |";
	out << endl;
	
	for (int k = 0; k < 7; k++){
		for (int l = 0; l < myHand.size; l++){
			out << "|        |";
		}
	out << endl;
	}
	
	for (int m = 0; m < myHand.size; m++)
		out << "|      " << myHand.hand[m] << "|";
	out << endl;

	for (int n = 0; n < myHand.size; n++)
		out << " -------- ";
	out << endl;

	return out;
	
}

//Card& operator[](int arrIndex){
//	return hand[arrIndex];
//}

//**********************************************************************************
void Hand::addCard(Card card){
	hand[index] = card;
	index++;
}

//**********************************************************************************
bool Hand::disCard(int position){
	if(position < index){
		hand[position] = hand[index];
		index--;
	return true;
	}
	else{
		return false;
	}
}

//**********************************************************************************
bool Hand::isEmpty(){
	if(index == 0)
		return true;
	else
		return false;
}

//**********************************************************************************
bool Hand::isFull(){
	if(index == size)
		return true;
	else
		return false;
}

handType Hand::findHand() //doesnt have a case for the Ace being a 1 yet, doesnt differentiate Jacks or Better for the pair
{
//-------------------------------------------------------------------------------------------------------------------
    handType tempHand = NOHAND;
	
	int temp;
	int cardArr[15];
	int arrSize = 15;
	
		//Initialize the array to all 0's
	for (int i=0; i < arrSize; i++){
		cardArr[i] = 0;
	}	

		//Make tick marks:
	for (int i=0; i < size; i++){
		temp = hand[i].getVal();

		switch (temp){
			case 2: cardArr[2] += 1;
				break;
			case 3: cardArr[3] += 1;
				break;
			case 4: cardArr[4] += 1;
				break;
			case 5: cardArr[5] += 1;
				break;
			case 6: cardArr[6] += 1;
				break;
			case 7: cardArr[7] += 1;
				break;
			case 8: cardArr[8] += 1;
				break;
			case 9: cardArr[9] += 1;
				break;
			case 10: cardArr[10] += 1;
				break;
			case 11: cardArr[11] += 1;
				break;
			case 12: cardArr[12] += 1;
				break;
			case 13: cardArr[13] += 1;
				break;
			case 14: cardArr[14] += 1; //special case for straights
				if(cardArr[1] == 0)
					cardArr[1] += 1;
				break;
		}
	}

	int set1 = 0;//two variables used to store the index of matching sets
	int set2 = 0;
	for (int i=0; i < arrSize; i++){
		if (cardArr[i] > 1 && set1 == 0){
			set1 = cardArr[i];
		}
		else if (cardArr[i] > 1){
			set2 = cardArr[i];
		}
	}

	//Put set 1 and set 2 in order (for easier comparison)
	if (set1 < set2){
		temp = set1;
		set1 = set2;
		set2 = temp;
	}//** Now set1 will always have 3 of a kind in case of a full house**

	//One/Two pair
	if (set1 == 2){
		if (set2 == 0){
			if(cardArr[11] > 1 || cardArr[12] > 1 || cardArr[13] > 1 || cardArr[14] > 1)
				tempHand = PAIR;//one pair
		}
		else {
			tempHand = TWO_PAIR;//two pair
		}
	}
	//three of a kind/full house
	if (set1 == 3){
		if (set2 == 0){
			tempHand = THREE_KIND;//3 of a kind
		}
		else {
			tempHand = FULL_HOUSE;//full house
		}
	}
	//four of a kind
	if (set1 == 4){
		tempHand = FOUR_KIND;//4 of a kind
	}

	bool isStraight = false;
    bool isFlush = false;

	
	
	//Check for Straight:
	for (int i=0; i < (arrSize - 4); i++){
		if (cardArr[i] == 1 && cardArr[(i+1)] == 1 && cardArr[(i+2)] == 1 && cardArr[(i+3)] == 1 && cardArr[(i+4)] == 1){
			isStraight = true;//straight (will check for straight flush after flush)
		}
	}

	//check for flush
    if (hand[1].getSuit() == hand[2].getSuit() && hand[2].getSuit() == hand[3].getSuit()
    && hand[3].getSuit() == hand[4].getSuit() && hand[4].getSuit() == hand[5].getSuit()) 
		isFlush = true;

	//assign flush/straight value
	if (isStraight == true)
        {
            if (isFlush == true)
                tempHand = STRAIGHT_FLUSH;
            else
                tempHand = STRAIGHT;
	}
	else if (isFlush == true)
		tempHand = FLUSH;

	return tempHand;







        //int temp_array[14];
        //for (int a = 0; a < 14; a++)
        //        temp_array[a] = 0;

        //int largest_amt = 0;
        //int second_largest_amt = 0;

        //int highest_card = temp_array[0];
        //int second_highest_card = 0;

        //bool isStraight = false;
        //bool isFlush = false;


        //for (int i = 0; i < index; i++)//load into array
        //{
        //     temp_array[hand[i].getVal()]++;
        //}

        //for (int j = 1; j < 14; j++)//find highest and second highest card value, highest and second highest card amounts
        //{
        //     if (temp_array[j] >= temp_array[highest_card])
        //        highest_card = j;

        //     largest_amt = temp_array[highest_card];

        //     if (temp_array[j] >= temp_array[second_highest_card] && temp_array[j] <= temp_array[highest_card])
        //        if (second_highest_card != highest_card && second_highest_card != 0)
        //        {
        //                second_highest_card = j;
        //                second_largest_amt = temp_array[second_highest_card];
        //        }


        //}

        //for (int s = 0; s < size-4; s++)//straight
        //{
        //     if (temp_array[s] != 0 && temp_array[s] == temp_array[s+1]+1 && temp_array[s+1] == temp_array[s+2]+1
        //         && temp_array[s+2] == temp_array[s+3]+1 && temp_array[s+3] == temp_array[s+4]+1)
        //         isStraight = true;
        //}

        //if (hand[1].getSuit() == hand[2].getSuit() && hand[2].getSuit() == hand[3].getSuit()
        //    && hand[3].getSuit() == hand[4].getSuit() && hand[4].getSuit() == hand[5].getSuit()) //check for flush
        //    isFlush = true;


        //if (isStraight == true)
        //{
        //    if (isFlush == true)
        //        tempHand = STRAIGHT_FLUSH;
        //    else
        //        tempHand = FLUSH;
        //}
        //else if (largest_amt == 4)
        //{
        //        tempHand = FOUR_KIND;
        //}
        //else if (largest_amt == 3)
        //{
        //     if (second_largest_amt == 2)
        //        tempHand = FULL_HOUSE;
        //     else
        //        tempHand = THREE_KIND;
        //}
        //else if (isFlush == true)
        //{
        //    tempHand = FLUSH;
        //}
        //else if (largest_amt == 2)
        //{
        //    if (second_largest_amt == 2)
        //        tempHand = TWO_PAIR;
        //    else
        //        tempHand = PAIR;
        //}
        //return tempHand;
//---------------------------------------------------------------------------------------------------------------------------
}
